package com.weibo.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.weibo.entity.JianceMail;
import com.weibo.service.JianceMailService;
import com.weibo.util.Page;

public class JianceMailServlet extends HttpServlet{

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String add = request.getParameter("add");
		String delete = request.getParameter("delete");
		String mail = request.getParameter("mail");
		String name = request.getParameter("name");
		String id = request.getParameter("id");
		
		JianceMailService ms = new JianceMailService();
		
		if("add".equals(add)){
			JianceMail m = new JianceMail();
			m.setMail(mail);
			m.setName(name);
			ms.add(m);
		}
		if("delete".equals(delete)){
			int idint = 0;
			if(id != null){
				idint = Integer.parseInt(id);
			}
			ms.delete(idint);
		}
		
		
		Page page = new Page();
		List<Object> list = ms.findAll();//获取全部数据
		int maxPage = page.getMaxPage(list, 10);//获取最大页码		
		
		String temp = request.getParameter("page");	//获取请求页码	
		int nowPage = page.getPage(list, 10, temp);//验证页码的有效性
		
		List<Object> initList = page.getInitPage(list, nowPage, 10);//获取指定页码的数据
		
		request.setAttribute("nowPage", nowPage);//当前页
		request.setAttribute("maxPage", maxPage);//最大页
		request.setAttribute("initlist", initList);//数据
		
		request.setAttribute("mailshu", list.size());//数据
		request.getRequestDispatcher("/jiance_mail.jsp").forward(request, response);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
